package com.javaweb.admin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.KdlSaleOutQc;
import com.javaweb.admin.entity.NcTaskLog;
import com.javaweb.admin.entity.SaleReturnInspectionResult;
import com.javaweb.admin.entity.Scheduled;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.query.KdlSaleOutQcQuery;
import com.javaweb.admin.query.KdlSaleOutQuery;
import com.javaweb.admin.query.KdlZ05kdlQuery;
import com.javaweb.admin.service.*;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.seegw.shaphar.IKDLService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.javaweb.admin.constant.KdlCallConstant.DOCTYPE.DOCTYPE_Z04KDL;
import static com.javaweb.admin.constant.KdlCallConstant.DOCTYPE.DOCTYPE_ZTPP;

@Component
public class SyncKdlSaleOut2NC extends AbstractJob {
    private static final Logger log = LoggerFactory.getLogger(SyncKdlPurchase2NC.class);
    @Autowired
    ScheduledMapper scheduledMapper;

    @Autowired
    IKDLService kdlService;

    @Autowired
    private IKdlSaleOutQcService kdlSaleOutQcService;


    @Autowired
    private IKdlSaleOutService kdlSaleOutService;

    @Autowired
    INcTaskLogService iNcTaskLogService;


    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();
    }

    @Override
    public void execute() {
        //获取康德乐采购入库质检回传
        KdlSaleOutQcQuery entity = new KdlSaleOutQcQuery();
        entity.setPrincipalid(CommonConfig.kdlCompanyId);
        entity.setDoctype(DOCTYPE_Z04KDL.value());
        entity.setStarttime(kdlSaleOutQcService.GetMaxSyncTime());
        entity.setEndtime(DateUtils.getTime());
        KdlSaleOutQuery saleOutQuery = new KdlSaleOutQuery();
        saleOutQuery.setPrincipalid(CommonConfig.kdlCompanyId);
        saleOutQuery.setDoctype(DOCTYPE_ZTPP.value());
        try {
            log.info("开始获取康德乐销售出库回传....");
            kdlSaleOutQcService.syncSaleOutQC(entity);
            kdlSaleOutService.syncSaleOut(saleOutQuery);
            log.info("获取康德乐销售出库回传完成");
        }
        catch(Exception ex){
            log.error(ex.getMessage());
        }
        //获取未完成上传的销售出库质检记录
        List<KdlSaleOutQc> returnList = kdlSaleOutQcService.getSaleOutSubmitList();
        NcTaskLog taskLog = new NcTaskLog();
        taskLog.setTaskType("销售出库回传NC");
        taskLog.setBillCount(returnList.size());
        taskLog.setStartTime(DateUtils.now());
        String billno = "";
        String remark = "";

        log.info(String.format("开始回写NC销售出库,获取到%d个待回写记录....",returnList.size()));
        JsonResult ret;
        for (KdlSaleOutQc result: returnList) {
            try {
                ret = kdlService.submitSaleOutQCToNC(result.getId());
                if(ret.getCode()==0){
                    remark = "销售订单:"+result.getVendorso()+"-出库质检批次:"+ result.getInspectionlot()+"回写NC成功";
                    log.info(remark);
                }
                else {
                    remark = "销售订单:"+result.getVendorso()+"-出库质检批次:"+ result.getInspectionlot()+"回写NC失败，错误:"+ret.getMsg();
                    log.info(remark);
                }
                if(!billno.contains(result.getVendorso())) {
                    billno += result.getVendorso() + ",";
                }
            }catch(Exception ex)
            {
                log.error(ex.getMessage());
            }
        }
        taskLog.setRemark(remark);
        taskLog.setBillNo(billno);
        taskLog.setEndTime(DateUtils.now());
        iNcTaskLogService.save(taskLog);


    }
}
